<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" :model="queryInfo" ref="subjectForm">
            <el-form-item label="学科名称">
              <el-input v-model="queryInfo.subjectName" size="small"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="removeInfo">清空</el-button>
              <el-button type="primary" size="small" @click="getList">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button style="float: right; padding: 10px" type="success" icon="el-icon-edit" size="small"
            @click="addSubjeck">新增学科
          </el-button>
        </el-col>
      </el-row>
      <!-- 消息提示区域 -->

      <el-alert type="info" show-icon :closable="false">
        <div slot="title">共 {{ total }} 条记录</div>
      </el-alert>

      <!-- 表格区域 -->
      <el-table :data="subjectList" style="width: 100%">
        <el-table-column label="序号" type="index" width="50px">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="120px">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="150px">
          <template v-slot="{row}">
            {{row.addDate | parseTimeByString}}
          </template>
        </el-table-column>

        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="120px">
          <template slot-scope="scope">
            <!-- {{scope.row.isFrontDisplay}} -->
            <span v-if="scope.row.isFrontDisplay==1">是</span>
            <span v-if="scope.row.isFrontDisplay==0">否</span>
          </template>
        </el-table-column>

        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签">
        </el-table-column>
        <el-table-column prop="totals" label="题目数量">
        </el-table-column>
        <el-table-column prop="address" label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="text" @click="skipDirectorys(scope.row)">学科分类</el-button>
            <el-button type="text" @click="skipTags(scope.row)">学科标签</el-button>
            <el-button type="text" @click="reviseList(scope.row)">修改</el-button>
            <el-button type="text" @click="removeList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.page" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout=" prev, pager, next, sizes, jumper" :total="total" style="margin-top: 20px; text-align: right;">
      </el-pagination>
    </el-card>
    <!-- 修改弹出的对话框 -->
    <el-dialog title="修改学科" :visible.sync="reviseDialogVisible" width="400px" @close="changeClose">
      <el-form :model="subjectForm" :rules="subjectFormRules" ref="subjectFormRef" label-width="100px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="subjectForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否修改">
          <el-switch v-model="subjectForm.isFrontDisplay" :active-value="1" :inactive-value="0" active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="reviseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增按钮的对话框 -->
    <el-dialog title="新增学科" :visible.sync="addDialogVisible" width="400px" @close='handleClose'>
      <el-form :model="addForm" :rules="subjectFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input placeholder="请输入学科名称" v-model="addForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="addForm.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeAddForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, update, add } from '@/api/hmmm/subjects.js'
export default {
  name: 'subjects',
  data () {
    return {
      // 开关是否修改
      // isFrontDisplay: null,
      // 点击切换修改对话框
      reviseDialogVisible: false,
      // 点击切换新增对话框
      addDialogVisible: false,
      // 获取的数据
      subjectList: [],
      // 查询参数
      queryInfo: {
        // 当前页数
        page: 1,
        // 页尺寸
        pagesize: 10,
        // 学科名称
        subjectName: ''
      },
      // 当前学科的数据总数
      total: 0,
      // 修改学科表单
      subjectForm: {
        // 学科名称
        subjectName: '',
        id: '',
        isFrontDisplay: ''
      },
      // 新增学科
      addForm: {
        subjectName: '',
        isFrontDisplay: true
      },
      // 学科校验规则
      subjectFormRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击跳转标签页面
    skipTags (row) {
      // console.log(row)
      this.$router.push({ path: '/subjects/tags', query: { id: row.id, subjectName: row.subjectName } })
    },
    // 点击跳转学科页面
    skipDirectorys (row) {
      // console.log(row)
      this.$router.push({ path: '/subjects/directorys', query: { id: row.id, subjectName: row.subjectName } })
    },
    // 点击新增学科确认
    async changeAddForm () {
      // console.log(123)
      const res = await add(this.addForm)
      if (res.status !== 200) return this.$message.error('失败')
      // console.log(res)
      this.$message.success('新增学科成功')
      this.addDialogVisible = false
      this.getList()
    },
    // 关闭修改弹框自定义校验
    changeClose () {
      this.$refs.subjectFormRef.resetFields()
    },
    // 关闭新增弹框自定义校验
    handleClose () {
      this.$refs.addFormRef.resetFields()
      this.addForm.subjectName = ''
      this.addForm.isFrontDisplay = true
    },
    // 点击显示新增对话框
    addSubjeck () {
      this.addDialogVisible = true
    },
    // 点击确定修改功能
    async changeInfo () {
      const res = await update(this.subjectForm)
      console.log(res)
      if (res.status !== 200) return this.$message.error('修改失败')
      this.$message.success('修改学科成功')
      this.reviseDialogVisible = false
      this.getList()
    },
    // 点击显示修改对话框
    reviseList (row) {
      // console.log(row)
      this.reviseDialogVisible = true
      this.subjectForm.subjectName = row.subjectName// row里面的值重新赋值
      this.subjectForm.id = row.id
      this.subjectForm.isFrontDisplay = row.isFrontDisplay
      // this.queryInfo.subjectName = row.subjectName
      // console.log(row)
    },
    // 删除功能
    removeList (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(123)
        const { data: res } = await remove({ id })
        console.log(res)
        // console.log('删除成功')
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 清除输入框列表
    removeInfo () {
      this.queryInfo.subjectName = ''
      this.getList()
    },
    // 显示当前页
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    // 每页显示多少条数据
    handleCurrentChange (newpage) {
      // console.log(newpage)
      this.queryInfo.page = newpage
      this.getList()
    },
    // 获取数据列表
    async getList () {
      const { data: res } = await list(this.queryInfo)
      // console.log(res)
      this.subjectList = res.items
      this.total = res.counts
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped lang='less'>
.el-card {
  margin: 10px;
}
.el-alert {
  margin-bottom: 15px;
}
// /deep/.el-dialog{
//   padding: 0;
// }
</style>
